/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-04-28
 * Time: 14:41
 */
public class HashBucket {
    static class Node {
        public int val;
        public int key;
        public Node next;

        public Node(int key,int val) {
            this.val = val;
            this.key = key;
        }
    }

    public Node[] array;
    public int usedSize;

    public double loadfactor = 0.75;

    public HashBucket() {
        array = new Node[10];
    }

    public void put(int key,int val) {
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                cur.val = val;
                return;
            }
            cur = cur.next;
        }
        Node newnode = new Node(key,val);
        newnode.next = cur;
        array[index] = newnode;
        usedSize++;

        if(lodeFacterCount() >= loadfactor) {
            resize();
        }

    }
    private void resize() {
        Node[] newArray = new Node[array.length * 2];
        for(int i = 0; i < array.length; i++) {
            Node cur = array[i];
            while(cur != null) {
                int newindex = cur.key % newArray.length;
                Node curN = cur.next;
                cur.next = newArray[newindex];
                newArray[newindex] = cur;
                cur = curN;
            }
        }
        array = newArray;

    }

    private double lodeFacterCount() {
        return this.usedSize*1.0/array.length;
    }

    public int getKey(int key) {
        int index = key % array.length;
        Node cur = array[index];
        while(cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
            cur = cur.next;
        }
        return -1;
    }


}
